<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$errors = array();

if (isset($_POST['action']) && $_POST['action'] == 'add')
{
	$name = UV::readMQ($route->POST("name"));
	$fix = UV::readMQ($route->POST("fix"));
	$parent = UV::readInt($route->POST("parent"));
	
	$query = "insert into `services` values(NULL,'$name','$fix','$parent')";
	
	$update = $db->sendQuery($query);
	if ($update)
		goToPage("/admin/services/edit/".mysql_insert_id());
	else
		$errors[] = "Ошибка добавления записи";
}

loadPage("head.php");
?>
<script type='text/javascript'>
$(document).ready(function(){
	$("#fix").change(function(){
		if ($("#name").val() != ""){
			var ex = $(this).val() == "none" ? $("#name").val() : (
				$(this).val() == "prefix" ? "Страхование "+$("#name").val().toLowerCase() : $("#name").val()+" страхование"
			);
			$("#example").html(ex);
		} else {
			$("#example").html("");
		}
	})
})
</script>
<h1>Добавление страховой услуги</h1><br /><?php
	if (count($errors) > 0)
		echo "<p class='fail'>".implode("<br />",$errors)."<br /><br /></p>";
?>
<form method='POST' id='edit'>
	<input type='hidden' name='action' value='add' />
	<table cellpadding='3' cellspacing='5'>
		<tr>
			<td width='170px'><label for='name'>Название</label></td>
			<td width='600px'><input type='text' id='name' name='name' value='<?php echo $route->POST('name'); ?>' /></td>
		</tr>
		<tr>
			<td><label for='fix'>Дописывать "страхование"</label></td>
			<td>
				<select class='short' id='fix' name='fix'><?php
					$services_arr = array(array("key"=>"none","value"=>"нет"),array("key"=>"prefix","value"=>"до"),array("key"=>"postfix","value"=>"после"));
					echo select_create($services_arr,"key","value",$route->POST('fix'));
				?></select>
				Пример: <span id='example'></span>
			</td>
		</tr>
		<tr>
			<td><label for='parent'>Относится к</label></td>
			<td>
				<select id='parent' name='parent'>
					<option value='0'>Не относится</option><?php
					$parent_services = $db->getTable("select * from `services` where `parent` = '0' order by `service_id`");
					echo select_create($parent_services,"service_id","name",$route->POST('parent'));
				?></select>
			</td>
		</tr>
		<tr>
			<td colspan='2' align='right'><input type='submit' value='Добавить' /></td>
		</tr>
	</table>
</form>

<?php loadPage("footer.php"); ?>